package com.umeox.moto.watch.themes.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.LayoutDirection;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.text.TextUtilsCompat;

import com.umeox.moto.watch.themes.R;
import com.umeox.moto.watch.themes.skin.listener.IDynamicNewView;
import com.umeox.moto.watch.themes.skin.loader.ResourceManager;

import java.util.Locale;


public class ActionOptionsDialogX extends Dialog implements View.OnClickListener {
    private Context mContext;
    private OnClickListener mOnClickListener;
    private int[] mDests;
    private int[] mIcons;


    private LinearLayout mContainer;
    /**
     * 动态界面换肤接口
     */
    private IDynamicNewView mDynamicNewView;

    public ActionOptionsDialogX(@NonNull Context context, int[] dests, int[] icons, IDynamicNewView dynamicNewView) {
        super(context);
        mContext = context;
        mDests = dests;
        mIcons = icons;
        mDynamicNewView = dynamicNewView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final View rootView = LayoutInflater.from(mContext).inflate(R.layout.dialog_base_x, null);
        setContentView(rootView);
        mContainer = rootView.findViewById(R.id.ll_container);
        initOptionView();
    }

    public void initOptionView() {
        for (int i = 0; i < mDests.length; i++) {

            final int dest = mDests[i];
            final int icon = mIcons[i];
            if (dest == -1) {
                break;
            }


            final View tvView = View.inflate(mContext, R.layout.view_action_option_item, null);
            mContainer.addView(tvView);

            final TextView tv = tvView.findViewById(R.id.tv_action_item);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 56);
            tv.setLayoutParams(layoutParams);
            tv.setOnClickListener(this);
            tv.setTag(i);
            tv.setText(dest);
            tv.setTextDirection(View.TEXT_DIRECTION_LOCALE);
            tv.setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);
            if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
                tv.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, ResourceManager.getInstance().getDrawable(icon), null);
            } else {
                tv.setCompoundDrawablesRelativeWithIntrinsicBounds(ResourceManager.getInstance().getDrawable(icon), null, null, null);
            }

//            List<DynamicAttr> mDynamicAttr = new ArrayList<DynamicAttr>();
//            mDynamicAttr.add(new DynamicAttr(AttrFactory.compoundList.get(0), icon));
//            mDynamicNewView.dynamicAddView(tv, mDynamicAttr);
        }
    }

    @Override
    public void onClick(View v) {
        final int id = (int) v.getTag();

        if (mOnClickListener == null) {
            dismiss();
            return;
        }
        switch (id) {

            case 0:
                mOnClickListener.onOption2Click();
                break;
            case 1:
                mOnClickListener.onOption3Click();
                break;
            case 2:
                mOnClickListener.onOption4Click();
                break;
            case 3:
                mOnClickListener.onOption5Click();
                break;

        }

        dismiss();
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public void setChildViewEnabled(int index, boolean enabled) {
        if (index < mContainer.getChildCount()) {
            mContainer.getChildAt(index).setEnabled(enabled);
        }
    }

    public LinearLayout getContainer() {
        return mContainer;
    }


    public interface OnClickListener {

        void onOption1Click();

        void onOption2Click();

        void onOption3Click();

        void onOption4Click();

        void onOption5Click();
    }
}
